package org.firefly.fx.service.user.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.firefly.fx.bean.TUser;
import org.firefly.fx.service.user.IUserService;
import org.firefly.fx.util.MD5Util;
import org.firefly.fx.util.RegExpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired
	private SqlSession sqlSession;
	
	public long insert(TUser user){
		sqlSession.insert("t_user.insert", user);
		return user.getId();
	}

	@Override
	public TUser login(String username, String password) {
		String sql = null;
		if(RegExpUtil.isEmail(username)){
			sql = "t_user.select_byEmailAndPassword";
		}
		
		if(RegExpUtil.IsTelephone(username)){
			sql = "t_user.select_byMobileAndPassword";
		}
		
		if(sql==null){//写成这样了，囧
			return null;
		}
		
		Map<String,String> params = new HashMap<String,String>();
		params.put("username", username);
		params.put("password", MD5Util.md5(password));
		
		TUser user = sqlSession.selectOne(sql, params);
		return user;
	}
}
